/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.vista.manteniment.proveidor;
import comandaXM.accions.ProveidorsAct;
import comandaXM.classes.manteniment.proveidor.CentreProveidor;
import comandaXM.classes.manteniment.proveidor.Contacte;
import comandaXM.excepcions.ExceptionHibernate;
import comandaXM.excepcions.ExceptionXML;
import comandaXM.utils.JOPtionPaneXM;
import comandaXM.utils.JPanelTaula;
import comandaXM.utils.TDSLanguageUtils;
import comandaXM.utils.TableModel;
import java.awt.event.ActionEvent;
import java.util.Iterator;
/**
 *
 * @author xmarti
 */
public class JPContactes  extends JPanelTaula{
    private CentreProveidor centreActual; 
    private Contacte contacteActual; 
    private JOPtionPaneXM optionPane= new JOPtionPaneXM(); 
    private ProveidorsAct proveidorAct= new ProveidorsAct(); 
    public JPContactes( CentreProveidor centre) throws ExceptionHibernate {
        super("jpContacteCentreProveidor");
        this.centreActual= centre; 
         
        super.chargeTable();        
    }

    @Override
    public void nou(ActionEvent evt) {
        try {            
            super.getFormatTable().setFormat();
            super.getFormatTable().saveFormat();
            contacteActual= new Contacte(); 
            contacteActual.setCentre(centreActual);            
            final JDContacte dialog = new JDContacte(null,true,contacteActual);               
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
            if(dialog.isActualitzar()){
                try {                     
                    centreActual.getContactes().add(contacteActual); 
                    this.chargeTable();
                } catch (ExceptionHibernate ex) {
                    optionPane.errMessagge(ex.getMessage());
                }
            }
            
        } catch (ExceptionXML ex) {
            
        }
        
    }

    @Override
    public void modificar(ActionEvent evt) {        
        this.contacteActual=(Contacte) this.getObject(); 
        final JDContacte dialog = new JDContacte(null,true,this.contacteActual);               
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        if (dialog.isActualitzar()){
            try {                 
                this.chargeTable();
            }catch (ExceptionHibernate ex) {
                this.optionPane.errMessagge(ex.getMessage());
            }
        }        
    }

    @Override
    public void eliminar(ActionEvent evt) {                        
        if(this.optionPane.ConfirmRemove()){
            try {
                this.centreActual.getContactes().remove((Contacte) this.getObject());                 
                this.proveidorAct.remove((Contacte) this.getObject());
                this.chargeTable();
            } catch (ExceptionHibernate ex) {                
                this.optionPane.errMessagge(ex.getMessage());                
            }
        }
    }

    @Override
    public TableModel getModel() throws ExceptionHibernate {
        TableModel dadesTaula = null;                            
            //TODO ESTIC AQUI CAL REDIFINIR ELS CENTRES. 
            String [] columns={                                                                                                         
                          TDSLanguageUtils.getMessage("jpContactes.JTable.nom.titol"),                          
                          TDSLanguageUtils.getMessage("jpContactes.JTable.Telefon1.titol"),                          
                          TDSLanguageUtils.getMessage("jpContactes.JTable.Telefon2.titol"),
                          TDSLanguageUtils.getMessage("jpContactes.JTable.fax.titol"),
                          TDSLanguageUtils.getMessage("jpContactes.JTable.email.titol"),
                          TDSLanguageUtils.getMessage("jpContactes.JTable.actiu.titol"),
                          };    
            Object[][] dades=null;                
            
            try{
                
                if (this.centreActual.getContactes().size()>0){
                    dades=new Object[this.centreActual.getContactes().size()][columns.length];                          
                    Integer i=0;            
                    for (Iterator it = this.centreActual.getContactes().iterator(); it.hasNext();) {
                        Contacte c = (Contacte) it.next();                
                        dades[i][0]= c;                           
                        if (c.getTelefon1() ==(null))dades[i][1]="-"; else dades[i][1]= c.getTelefon1(); 
                        if (c.getTelefon2() ==(null))dades[i][2]="-"; else dades[i][2]= c.getTelefon2(); 
                        if (c.getFax() ==(null))dades[i][3]="-"; else dades[i][3]= c.getFax(); 
                        if (c.getEmail() ==(null))dades[i][4]="-"; else dades[i][4]= c.getEmail(); 
                        dades[i][5]= c.isActiu(); 
                        i++;
                    }     
                }                  
            }catch(NullPointerException e){
                
            }
            
        return new TableModel(columns, dades); 
    }

    @Override
    public void select() {
       
    }
    
}
